﻿using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace revitLinkDemo
{
    [Transaction(TransactionMode.Manual)]
    public class getgridname : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            var uiDoc = commandData.Application.ActiveUIDocument;
            var doc = uiDoc.Document;


            string[] Name = new string[4];
            string gridName1;
            string gridName2;
            string gridName;
            var _selctionFilter = new selctionFilter();

            
            
     

            //var selection = uiDoc.Selection.PickElementsByRectangle(new selctionFilter()); 矩形区域选中
            try
            {
                for (int i = 0; i < 4; i++)
                {

                    var reference = uiDoc.Selection.PickObject(ObjectType.LinkedElement, _selctionFilter);
                    var linkDoc = _selctionFilter.linkDoc;
                    var seletion = linkDoc.GetElement(reference.LinkedElementId);
                    var grid = seletion as Grid;
                    Name[i] = grid.Name;
                }
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {


            }

            if (Name[0] == Name[1])
            {
                gridName1 = Name[1];
            }
            else
            {
                gridName1 = Name[0] + "~" + Name[1];
            }
            if (Name[2] == Name[3])
            {
                gridName2 = Name[2];
            }
            else
            {
                gridName2 = Name[2] + "~" + Name[3];
            }
            gridName = gridName1 + "交" + gridName2 + "轴";
            //foreach (var item in selection)
            //{

            //    TaskDialog.Show("zzs", item.Name);
            //}
            //TaskDialog.Show("zzs", gridName);
            Clipboard.SetText(gridName);
            return Result.Succeeded;
        }
    }
    public class selctionFilter : ISelectionFilter
    {
         public Document linkDoc = null;
        Element element = null;

        public bool AllowElement(Element elem)
        {


            if (elem is RevitLinkInstance)
            {
                var revitLinkInstance = elem as RevitLinkInstance;
                linkDoc = revitLinkInstance.GetLinkDocument();
                var element = linkDoc.GetElement(revitLinkInstance.Id);
                return true;
            }
            return false;
            //if (element.Id.IntegerValue ==(int)BuiltInCategory.OST_Grids )
            //{
            //    return true;
            //}
            //return false;



        }

        bool ISelectionFilter.AllowReference(Reference reference, XYZ position)
        {
            if (linkDoc.GetElement(reference.LinkedElementId) is Grid)
            {
                return true;
            }
            return false;
        }
    }


}

